<template>
  <div class="users-management">
    <div class="header">
      <h3>用户管理</h3>
      <el-button type="primary" @click="showAddDialog">新增用户</el-button>
    </div>

    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="role" label="角色">
        <template #default="{row}">
          <el-tag :type="roleTagType[row.role]">{{ roleMap[row.role] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{row}">
          <el-button size="small" @click="editUser(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteUser(row.phone)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" label-width="80px" ref="formRef">
        <el-form-item label="姓名" :rules="{ required: true, message: '请输入姓名', trigger: 'blur' }">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="手机号" :rules="{ required: true, pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role">
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()
const userList = computed(() => authStore.users)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = reactive({
  name: '',
  phone: '',
  role: 'student'
})
const formRef = ref(null)
const currentEditId = ref('')

const roleOptions = [
  { value: 'student', label: '学生' },
  { value: 'teacher', label: '教师' },
  { value: 'admin', label: '管理员' }
]

const roleMap = {
  student: '学生',
  teacher: '教师',
  admin: '管理员'
}

const roleTagType = {
  student: 'info',
  teacher: 'success',
  admin: 'primary'
}

const showAddDialog = () => {
  dialogTitle.value = '新增用户'
  form.name = ''
  form.phone = ''
  form.role = 'student'
  currentEditId.value = ''
  dialogVisible.value = true
}

const editUser = (row) => {
  dialogTitle.value = '编辑用户'
  form.name = row.name
  form.phone = row.phone
  form.role = row.role
  currentEditId.value = row.phone
  dialogVisible.value = true
}

const deleteUser = (phone) => {
  if (phone === '18888888888' || phone === '19999999999') {
    ElMessage.error('系统账号不可删除')
    return
  }
  authStore.users = authStore.users.filter(u => u.phone !== phone)
  authStore.persistUsers()
  ElMessage.success('删除成功')
}

const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.phone === '18888888888' || form.phone === '19999999999') {
        ElMessage.error('系统保留号码不可使用')
        return
      }
      if (currentEditId.value) {
        // 更新逻辑
        const index = authStore.users.findIndex(u => u.phone === currentEditId.value)
        if (index!== -1) {
          authStore.users.splice(index, 1, { ...form })
        }
      } else {
        // 新增逻辑
        authStore.registerUser({ ...form })
      }
      authStore.persistUsers()
      dialogVisible.value = false
      ElMessage.success('操作成功')
    } else {
      ElMessage.error('请填写完整信息')
    }
  })
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>